package wangyachuan.sptingboot.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;
import wangyachuan.sptingboot.common.Result;
import wangyachuan.sptingboot.common.page;
import wangyachuan.sptingboot.model.user;
import wangyachuan.sptingboot.server.UserServer;


import java.util.List;

@CrossOrigin// 允许跨域请求
@SuppressWarnings("all")
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired// 自动注入UserServer对象
    UserServer userServer;// 注入UserServer对象


    // 添加用户
    @PostMapping("/add")
    public Result add(@RequestBody user user) {
        try {
            userServer.insertUser(user);
        } catch (Exception e) {
            if (e instanceof DuplicateKeyException) {
                return Result.error("插入数据库失败");
            } else {
                return Result.error("系统错误");
            }
        }
        return Result.success();
    }

    //修改用户信息
    @PutMapping("/update")
    public Result update(@RequestBody user user) {
        userServer.updateUser(user);
        return Result.success();

    }

    //删除用户
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        userServer.deleteUser(id);
        return Result.success();
    }

    //批量删除用户
    @DeleteMapping("/delete/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        userServer.deleteUserBatch(ids);
        return Result.success();
    }

    //查询用户信息
    @GetMapping("/selectID/{id}")
    public Result get(@PathVariable Integer id) {
        user user = userServer.selectID(id);
        return Result.success(user);
    }

    //查询所有用户信息
    @GetMapping("/selectAll")
    public Result selectAll() {
        List<user> userList = userServer.selectAll();
        return Result.success(userList);
    }

    //模糊查询用户name查询信息
    @GetMapping("/selectByName/{username}")
    public Result selectByName(@PathVariable String username) {
        List<user> userList = userServer.selectByName(username);
        return Result.success(userList);
    }

    //多条件查询用户信息
    @GetMapping("/selectByMore")
    public Result selectByMore(@RequestParam String username, @RequestParam String name) {
        List<user> userList = userServer.selectByMore(username, name);
        return Result.success(userList);
    }

    //多条件查询用户信息，模糊查询
    @GetMapping("/selectByMoreAndName")
    public Result selectByMoreAndName(@RequestParam String username, @RequestParam String name) {
        List<user> userList = userServer.selectByMoreAndName(username, name);
        return Result.success(userList);
    }

    //分页多条件模糊查询
    //@RequestParam Integer pageNum, 当前页码
    //@RequestParam Integer pageSize, 每页显示条数
    //@RequestParam String username, 用户名
    //@RequestParam String name, 姓名
    @GetMapping("/selectByPage")
    public Result selectByPage(
            @RequestParam Integer pageNum,
            @RequestParam Integer pageSize,
            @RequestParam String username,
            @RequestParam String name) {
        page<user> page = userServer.selectByPage(pageNum, pageSize, username, name);
        return Result.success(page);
    }
}
